﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using EOS.Framework.Web.Mvc.Rendering;
using System.Diagnostics.CodeAnalysis;

namespace EOS.Framework.Web.Mvc.Controls
{
    public class ImageHyperlink : Control
    {
        private const string DefaultImageClass = "icon";

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _href;

        public string Href
        {
            get { return this._href; }
            set { this._href = value; }
        }

        private string _alt;

        public string Alt
        {
            get { return _alt; }
            set { _alt = value; }
        }

        private string _image;

        public string Image
        {
            get { return _image; }
            set { _image = value; }
        }

        private string _imageClass = DefaultImageClass;

        public string ImageClass
        {
            get { return _imageClass; }
            set { _imageClass = value; }
        }

        public ImageHyperlink(string href, string name, string image)
            : this(href, name, image, null)
        { }

        public ImageHyperlink(string href, string name, string image, string alt)
            : this(href, name, image, alt, null)
        { }

        public ImageHyperlink(string href, string name, string image, string alt, string imageClass)
        {
            this._image = image;
            this._imageClass = imageClass;
            this._href = href;
            this._name = name;
            this._alt = alt;
        }

        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower(CultureInfo.CurrentCulture).Equals("a", StringComparison.CurrentCulture) == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "img"));
            }

            WriteBeginAttachedTag(writer, new string[] { "href" });
            writer.WriteAttribute("href", _href);

            writer.WriteStartElement("img");
            writer.WriteAttribute("src", _image);

            if (!string.IsNullOrEmpty(_alt))
            {
                writer.WriteAttribute("alt", _alt);
            }
            if (!string.IsNullOrEmpty(_imageClass))
            {
                writer.WriteAttribute("class", _imageClass);
            }
            writer.WriteEndElement();

            if (!string.IsNullOrEmpty(_name))
            {
                writer.WriteString(_name);
            }

            WriteEndAttachedTag(writer);
        }
    }
}
